<template>
  <div>
    <div style="display: flex;justify-content: space-around;">
      <el-row>
        <el-col>
          <div class="grid-content bg-purple-dark">
            <el-table
                :data="tableData"
                highlight-current-row
                style="width: 100%"
                @current-change="handleCurrentChange">
              <el-table-column
                  label="编号"
                  prop="purchaseId"
                  width="360">
              </el-table-column>
              <el-table-column
                  label="供应商名称"
                  prop="supplierBean.supplierName"
                  width="360">
              </el-table-column>
              <el-table-column
                  label="采购者"
                  prop="userBean.username"
                  width="360">
              </el-table-column>
              <el-table-column
                  label="采购时间"
                  prop="purchaseDate"
                  width="360">
              </el-table-column>
            </el-table>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <div class="block">
            <el-pagination
                :current-page=currentPage
                :page-size=pageSize
                :total=totalSize
                background
                layout="prev, pager, next"
                @current-change="currentPageFn">
            </el-pagination>
          </div>
        </div>
      </el-col>
    </el-row>


    <el-dialog
        :visible.sync="showInfo"
        title="采购信息详情"
        width="70%">
      <el-form label-width="80px">
        <PInfoDetail ref="Child"></PInfoDetail>
      </el-form>
    </el-dialog>


    <div style="display: flex;justify-content: center">
      <el-row style="margin: 25px;width: 220px">
        <el-input v-model="searchName" placeholder="供应商名称" style="margin-bottom: 10px"></el-input>
        <el-date-picker
            v-model="searchScope"
            placeholder="起始时间"
            style="margin-bottom: 10px"
            type="date"
            value-format="yyyy-MM-dd">
        </el-date-picker>
        <el-date-picker
            v-model="searchScope2"
            placeholder="结束时间"
            type="date"
            value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-row>
      <el-row style="margin-top: 50px">
        <el-button plain type="primary" @click="findByItem">查询</el-button>
        <el-button plain type="primary" @click="findByDetail">查看采购详情</el-button>
      </el-row>
    </div>

  </div>
</template>

<script>
import PInfoDetail from "./PInfoDetail";

export default {
  name: "PurchaseInfo",
  components: {PInfoDetail},
  data() {
    return {
      tableData: [],
      totalSize: 0,//总行数
      pageSize: 0,//当前显示数据页
      currentPage: 0,//当前页码
      currentRow: 0,//当前选中列
      searchName: '',
      searchScope: '',
      searchScope2: '',
      showInfo: false
    }
  },
  mounted() {
    this.showPage()
  },
  methods: {
    currentPageFn(currentPage) {
      this.currentPage = currentPage
      this.showPage()
    },
    handleCurrentChange(val) {
      //必须不为null才能进行跳转分页
      if (val != null) {
        this.currentRow = val;
      }
    },
    showPage(num) {
      if (this.searchName === '') {
        this.searchName = 1
      }
      if (this.searchScope === '') {
        this.searchScope = "1900-01-01"
      }
      if (this.searchScope2 === '') {
        this.searchScope2 = "2999-01-01"
      }

      let url = "api/purchase/showByPage/" + this.currentPage + "/" + this.searchName + "/" + this.searchScope + "/" + this.searchScope2
      this.axios.get(url).then(
          r => {
            this.tableData = r.data.list
            this.totalSize = r.data.total
            this.pageSize = r.data.pageSize
            if (num == 1) {
              this.messageShow("查询成功", "请查看结果")
            }
          }
      )
      this.searchName = ''
      this.searchScope = ''
      this.searchScope2 = ''
    },
    findByItem() {
      this.showPage(1)
    },
    findByDetail() {
      // console.log(this.currentRow)
      if (this.currentRow == 0) {
        this.messageShow("", "请先选中要查看的采购项")
        return
      }
      this.showInfo = true
      let purchaseId = this.currentRow.purchaseId
      //调用子组件进行方法显示
      setTimeout(() => {
        this.$refs.Child.showPurchaseByDetail(purchaseId);
      }, 10)
      this.currentRow = 0
    },
    messageShow(title, message) {
      this.$notify({
        title: title,
        message: (message)
      });
    }
  }
}
</script>

<style scoped>

</style>